/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package parser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import wiiscript.Main;
import wiiscript.Properties;

/**
 *
 * @author Evan
 */
public class ParserInterface {
    private HashMap<String, Object> variables = new HashMap<String, Object>();
    private ArrayList<ChangeVariableListener> varListeners = new ArrayList<ChangeVariableListener>();
    private Main main;
    private Node root = new Node(Node.ROOT);
    private Node currentNode = root;
    public ParserInterface(Main main) {
        this.main = main;
        populateVars();
    }
    private void populateVars() {
        String[] values = Properties.getProperties();
        for(String v: values) {
            putVariable(v.toLowerCase(), false, false); //Do not emit change variable events.
        }
    }
    public void setDebug(String value) {
        main.setDebug(value);
    }
    private synchronized void putVariable(String name, Object value, boolean emitChanges) {
        name = name.toLowerCase();
        if(emitChanges && (!variables.containsKey(name) || variables.get(name) != value)) {
            changeVar(name, value);
        }
        variables.put(name, value);
        if(name.toLowerCase().equals("debug")) {
            setDebug(value.toString());
        }
    }
    public synchronized void putVariable(String name, Object value) {
        putVariable(name, value, true); //Emit change variable events.
    }
    public synchronized Object getVariable(String name) {
        return variables.get(name.toLowerCase());
    }
    public synchronized String[] getVariableNames() {
        Set varSet = variables.entrySet();
        Entry[] entries = (Entry[]) varSet.toArray(new Entry[varSet.size()]);
        String[] varNames = new String[entries.length];
        for(int i = 0; i < entries.length; i++) {
            varNames[i] = (String)entries[i].getKey();
        }
        return varNames;
    }
    public void resetVariables() {
        variables.clear();
        populateVars();
    }
    public void clearTree() {
        root = new Node(Node.ROOT);
        currentNode = root;
    }
    public void addToken(Token t) {
        Node n = new Node(t);
        currentNode.add(n, currentNode.kind);
    }
    public void addToken(int name) {
        Node n = new Node(name);
        currentNode.add(n, currentNode.kind);
        currentNode = n;
    }
    public void endNode() {
        currentNode = currentNode.parent;
    }
    public void printTree() {
        System.out.println(root);
    }
    public void parse() {
        root.parse(this);
    }
    public void addChangeVariableListener(ChangeVariableListener listener) {
        varListeners.add(listener);
    }
    private void changeVar(String name, Object value) {
        VarEvent e = new VarEvent(name, value, this);
        for(ChangeVariableListener l : varListeners) {
            l.variableChanged(e);
        }
    }
}
